package day01;

import java.time.LocalDateTime;

/**
 * @author feng
 * @Date 2021/7/18 8:07
 * 线程的结束
 */
public class ThreadTest08 {

    static class MyTimer implements Runnable {
        boolean flag = true;   //定义一个标志变量

        @Override
        public void run() {
            while (flag) {
                System.out.println("" + LocalDateTime.now() + "...");
                try {
                    Thread.sleep(1000);        // 睡一秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("" + Thread.currentThread().getName() + "结束");

            }
        }

        public void StopRun() {            //将标志标记改为false
            flag = false;
        }


    }                       // 内部类结束

    public static void main(String[] args) {
        MyTimer t = new MyTimer();
        Thread t1 = new Thread(t, "t1");
        Thread t2 = new Thread(t, "t2");
        t1.start();
        t2.start();
        for (int i = 0; i < 100; i++) {
            System.out.println("" + i);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            t.StopRun();               // 是用户线程结束
        }


    }

}
